package sessions;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entities.PropostaAbilita;

@Stateless
public class PropostaAbilitaManager implements PropostaAbilitaManagerRemote{

	@PersistenceContext(unitName = "SwimDB")
	EntityManager database;

	@Override
	public List<PropostaAbilita> getProposteAbilita() {
		List<PropostaAbilita> risultato = new ArrayList<PropostaAbilita>();
		
		try {
			Query q = database.createQuery("FROM PropostaAbilita p");
			risultato=q.getResultList();
			
			System.out.println("*** list size *** " + risultato.size());
			
		} catch (EntityNotFoundException exc) { 
		} catch (NonUniqueResultException exc) {
		}
		
		return risultato;
	}

	@Override
	public String getDescrizioneFromName(String nome) {
		String risultato=null;
		try {
			Query q = database.createQuery("SELECT p.descrizione FROM PropostaAbilita p where p.nomeAbilita=:nome");
			q.setParameter("nome",nome);
			risultato=(String) q.getSingleResult();
			
		
		} catch (EntityNotFoundException exc) { 
		} catch (NonUniqueResultException exc) {
		}
		
		return risultato;
	}
	
	public void removeProposteAbilita()
	{
		try {
			for(PropostaAbilita x: getProposteAbilita())
				database.remove(x);
			
		
		} catch (EntityNotFoundException exc) { 
		} catch (NonUniqueResultException exc) {
		}
	}

}
